package mapEditor.view
{
	import asset.mapEditorItemEditToolBarAsset;
	import asset.mapEditorSceneEditToolBarAsset;
	import asset.mapEditorTopAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.ModuleType;
	import mapEditor.vo.ToolType;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;

	public class TopPanelView extends mapEditorTopAsset
	{
		private var _isWinMax:Boolean;
		private var _mapEditorItemEditToolBarAsset:mapEditorItemEditToolBarAsset;
		private var _mapEditorSceneEditToolBarAsset:mapEditorSceneEditToolBarAsset;
		
		public function TopPanelView()
		{
			__onStateResize(null);
			
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			mcMapBar.visible=false;
			btnResEdit.buttonMode=btnSceneEdit.buttonMode=btnScenePreview.buttonMode=true;
			mcResBar.btnAssetItemEdit.buttonMode=mcResBar.btnResItemEdit.buttonMode=mcResBar.btnResComEdit.buttonMode=mcResBar.btnResArea.buttonMode=mcResBar.btnCreateRes.buttonMode=mcMapBar.btnTerrainEdit.buttonMode=mcMapBar.btnGeomorphologyEdit.buttonMode=true;
			mcResBar.btnCreateRes.txtName.mouseEnabled=false;
			
			btnResEdit.gotoAndStop(1);
			btnSceneEdit.gotoAndStop(2);
			btnScenePreview.gotoAndStop(2);
			mcTool.btnSelect.gotoAndStop(1);
			mcTool.btnMove.gotoAndStop(1);
			mcTool.btnZoomIn.gotoAndStop(1);
			mcTool.btnZoomOut.gotoAndStop(1);
		}
		
		private function addEvent():void
		{
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
			StageReflection.stage.addEventListener(Event.RESIZE, __onStateResize);
			mcTopBg1.doubleClickEnabled=mcTopBg2.doubleClickEnabled=mcTopBg3.doubleClickEnabled=true;
			mcTopBg1.addEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg2.addEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg3.addEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg1.addEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcTopBg2.addEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcTopBg3.addEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcWinBtn.btnMin.buttonMode=mcWinBtn.btnMax.buttonMode=mcWinBtn.btnClose.buttonMode=true;
			mcWinBtn.btnMin.addEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			mcWinBtn.btnMax.addEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			mcWinBtn.btnClose.addEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			btnMapCreate.buttonMode=btnMapOpen.buttonMode=btnMapSave.buttonMode=true;
			btnMapCreate.addEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnMapOpen.addEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnMapSave.addEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnResEdit.addEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			btnSceneEdit.addEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			btnScenePreview.addEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			mcResBar.btnAssetItemEdit.addEventListener(MouseEvent.CLICK, __onResBtnMouseClick);			
			mcResBar.btnResItemEdit.addEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			mcResBar.btnResComEdit.addEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			mcResBar.btnResArea.addEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			
			mcMapBar.btnTerrainEdit.addEventListener(MouseEvent.CLICK, __onMapBtnMouseClick);
			mcMapBar.btnGeomorphologyEdit.addEventListener(MouseEvent.CLICK, __onMapBtnMouseClick);
			
			mcTool.btnSelect.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnMove.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnZoomIn.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnZoomOut.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
		}
		
		private function removeEvent():void
		{
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStateResize);
			mcTopBg1.removeEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg2.removeEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg3.removeEventListener(MouseEvent.MOUSE_DOWN, __onTopBgMouseEvent);
			mcTopBg1.removeEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcTopBg2.removeEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcTopBg3.removeEventListener(MouseEvent.DOUBLE_CLICK, __onTopBgMouseEvent);
			mcWinBtn.btnMin.removeEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			mcWinBtn.btnMax.removeEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			mcWinBtn.btnClose.removeEventListener(MouseEvent.CLICK, __onWinBtnMouseClick);
			btnMapCreate.removeEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnMapOpen.removeEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnMapSave.removeEventListener(MouseEvent.CLICK, __onBtnMouseClick);
			btnResEdit.removeEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			btnSceneEdit.removeEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			btnScenePreview.removeEventListener(MouseEvent.CLICK, __onColumnBtnMouseClick);
			mcResBar.btnAssetItemEdit.removeEventListener(MouseEvent.CLICK, __onResBtnMouseClick);	
			mcResBar.btnResItemEdit.removeEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			mcResBar.btnResComEdit.removeEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			mcResBar.btnResArea.removeEventListener(MouseEvent.CLICK, __onResBtnMouseClick);
			mcMapBar.btnTerrainEdit.removeEventListener(MouseEvent.CLICK, __onMapBtnMouseClick);
			mcMapBar.btnGeomorphologyEdit.removeEventListener(MouseEvent.CLICK, __onMapBtnMouseClick);
			mcTool.btnSelect.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnMove.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnZoomIn.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
			mcTool.btnZoomOut.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
		}
		
		private function __onResModuleTypeChange(evt:MapEditorEvent):void
		{
			setModuleInfo();
		}
		private function __onSceneModuleTypeChange(evt:MapEditorEvent):void
		{
			setModuleInfo();
		}		
		
		private function setModuleInfo():void
		{
			clearToolAsset();
			
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.resModuleType)
					{
						case ModuleType.RES_ITEM_EDIT:
							_mapEditorItemEditToolBarAsset=new mapEditorItemEditToolBarAsset();
							_mapEditorItemEditToolBarAsset.btnGridSelect.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
							_mapEditorItemEditToolBarAsset.y=mcTool.y;
							_mapEditorItemEditToolBarAsset.x=mcTool.x-_mapEditorItemEditToolBarAsset.width-5;
							addChild(_mapEditorItemEditToolBarAsset);
							break;
					}
					break;
				case ModuleType.SCENE_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.sceneModuleType)
					{
						case ModuleType.SCENE_TERRAIN_EDIT:
							_mapEditorSceneEditToolBarAsset=new mapEditorSceneEditToolBarAsset();
							_mapEditorSceneEditToolBarAsset.btnGridSelect.addEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
							_mapEditorSceneEditToolBarAsset.y=mcTool.y;
							_mapEditorSceneEditToolBarAsset.x=mcTool.x-_mapEditorSceneEditToolBarAsset.width-5;
							addChild(_mapEditorSceneEditToolBarAsset);
							break;
					}
					break;
			}
		}
		
		private function __onItemEditToolBtnMouseClick(evt:MouseEvent):void
		{
			_mapEditorItemEditToolBarAsset.btnGridSelect.gotoAndStop(1);
			switch(evt.currentTarget)
			{
				case _mapEditorItemEditToolBarAsset.btnGridSelect://网格选择器
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_GRID_SELECT;
					_mapEditorItemEditToolBarAsset.btnGridSelect.gotoAndStop(2);
					break;
			}
		}
		
		private function __onSceneEditToolBtnMouseClick(evt:MouseEvent):void
		{
			_mapEditorSceneEditToolBarAsset.btnGridSelect.gotoAndStop(1);
			switch(evt.currentTarget)
			{
				case _mapEditorSceneEditToolBarAsset.btnGridSelect://网格选择器
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_GRID_SELECT;
					_mapEditorSceneEditToolBarAsset.btnGridSelect.gotoAndStop(2);
					break;
			}
		}		
		
		private function __onColumnBtnMouseClick(evt:MouseEvent):void
		{
			btnResEdit.gotoAndStop(2);
			btnSceneEdit.gotoAndStop(2);
			btnScenePreview.gotoAndStop(2);
			evt.target.gotoAndStop(1);
			switch(evt.target)
			{
				case btnResEdit:
					mcResBar.visible=true;
					mcMapBar.visible=false;
					MapEditorManager.instance.mapEditorModel.moduleType=ModuleType.RES_EDIT;
					MapEditorManager.instance.mapEditorModel.resModuleType=ModuleType.RES_ASSET_EDIT;
					break;
				case btnSceneEdit:
					mcResBar.visible=false;
					mcMapBar.visible=true;
					MapEditorManager.instance.mapEditorModel.moduleType=ModuleType.SCENE_EDIT;
					MapEditorManager.instance.mapEditorModel.sceneModuleType=ModuleType.SCENE_TERRAIN_EDIT;
					break;
				case btnScenePreview:
					mcResBar.visible=false;
					mcMapBar.visible=false;
					MapEditorManager.instance.mapEditorModel.moduleType=ModuleType.SCENE_PREVIEW;
					break;
			}
		}
		
		private function __onResBtnMouseClick(evt:MouseEvent):void
		{
			switch(evt.target)
			{
				case mcResBar.btnAssetItemEdit:
					mcResBar.btnCreateRes.visible=false;
					MapEditorManager.instance.mapEditorModel.resModuleType=ModuleType.RES_ASSET_EDIT;
					break;			
				case mcResBar.btnResItemEdit:
					mcResBar.btnCreateRes.visible=true;
					mcResBar.btnCreateRes.txtName.text="单项物件创建";
					MapEditorManager.instance.mapEditorModel.resModuleType=ModuleType.RES_ITEM_EDIT;
					break;
				case mcResBar.btnResComEdit:
					mcResBar.btnCreateRes.visible=true;
					mcResBar.btnCreateRes.txtName.text="组合物件创建";
					break;
				case mcResBar.btnResArea:
					mcResBar.btnCreateRes.visible=true;
					mcResBar.btnCreateRes.txtName.text="区域物件创建";
					break;
			}
		}
		
		private function __onMapBtnMouseClick(evt:MouseEvent):void
		{
			switch(evt.target)
			{
				case mcMapBar.btnTerrainEdit:
					MapEditorManager.instance.mapEditorModel.sceneModuleType=ModuleType.SCENE_TERRAIN_EDIT;
					break;			
				case mcMapBar.btnGeomorphologyEdit:
					MapEditorManager.instance.mapEditorModel.sceneModuleType=ModuleType.SCENE_GEOM0RPHOLOGY_EDIT;
					break;
			}
		}
		
		private function __onBtnMouseClick(evt:MouseEvent):void
		{
			switch(evt.target)
			{
				case btnMapCreate:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.CREATE_MAP;
					break;
				case btnMapOpen:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.OPEN_MAP;
					break;
				case btnMapSave:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.SAVE_MAP;
					break;
			}
		}
		
		private function __onToolBtnMouseClick(evt:MouseEvent):void
		{
			mcTool.btnSelect.gotoAndStop(1);
			mcTool.btnMove.gotoAndStop(1);
			mcTool.btnZoomIn.gotoAndStop(1);
			mcTool.btnZoomOut.gotoAndStop(1);
			evt.target.gotoAndStop(2);
			switch(evt.target)
			{
				case mcTool.btnSelect:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_SELECT;
					break;
				case mcTool.btnMove:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_MOVE;
					break;
				case mcTool.btnZoomIn:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_ZOOM_IN;
					break;
				case mcTool.btnZoomOut:
					MapEditorManager.instance.mapEditorModel.toolType=ToolType.TOOL_ZOOM_OUT;
					break;
			}
			if(_mapEditorItemEditToolBarAsset)__onItemEditToolBtnMouseClick(evt);
			if(_mapEditorSceneEditToolBarAsset)__onSceneEditToolBtnMouseClick(evt);			
		}
		
		private function __onWinBtnMouseClick(evt:MouseEvent):void
		{
			switch(evt.target)
			{
				case mcWinBtn.btnMin:
					StageReflection.stage.nativeWindow.minimize();
					break;
				case mcWinBtn.btnMax:
					if(_isWinMax)
					{
						StageReflection.stage.nativeWindow.restore();
						_isWinMax=false;
					}
					else
					{
						StageReflection.stage.nativeWindow.maximize();
						_isWinMax=true;
					}
					break;
				case mcWinBtn.btnClose:
					StageReflection.stage.nativeWindow.close();
					break;
			}
		}
		
		private function __onTopBgMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.MOUSE_DOWN:
					StageReflection.stage.nativeWindow.startMove();
					break;
				case MouseEvent.DOUBLE_CLICK:
					if(_isWinMax)
					{
						StageReflection.stage.nativeWindow.restore();
						_isWinMax=false;
					}
					else
					{
						StageReflection.stage.nativeWindow.maximize();
						_isWinMax=true;
					}
					break;
			}
		}
		
		private function __onStateResize(evt:Event):void
		{
			mcTopBg1.x=0;
			mcTopBg2.width=StageReflection.stageWidth-mcTopBg1.width-mcTopBg3.width;
			mcTopBg2.x=mcTopBg1.width;
			mcTopBg3.x=StageReflection.stageWidth-mcTopBg3.width;
			mcWinBtn.x=StageReflection.stageWidth-mcWinBtn.width-6;
			mcBarBg.x=2;
			mcBarBg.width=StageReflection.stageWidth-4;
			mcTool.x=StageReflection.stageWidth-mcTool.width-15;
			if(_mapEditorItemEditToolBarAsset)_mapEditorItemEditToolBarAsset.x=mcTool.x-_mapEditorItemEditToolBarAsset.width-5;
			if(_mapEditorSceneEditToolBarAsset)_mapEditorSceneEditToolBarAsset.x=mcTool.x-_mapEditorSceneEditToolBarAsset.width-5;
		}
		
		private function clearToolAsset():void
		{
			if(_mapEditorItemEditToolBarAsset)
			{
				_mapEditorItemEditToolBarAsset.btnGridSelect.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
				DisplayUtil.dispose(_mapEditorItemEditToolBarAsset);
				_mapEditorItemEditToolBarAsset=null;
			}
			if(_mapEditorSceneEditToolBarAsset)
			{
				_mapEditorSceneEditToolBarAsset.btnGridSelect.removeEventListener(MouseEvent.CLICK, __onToolBtnMouseClick);
				DisplayUtil.dispose(_mapEditorSceneEditToolBarAsset);
				_mapEditorSceneEditToolBarAsset=null;
			}
		}
		
		public function dispose():void
		{
			removeEvent();
			if(parent)parent.removeChild(this);
		}
	}
}